package com.fjl.interceptor;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * TOOD
 *
 * @author luo
 * @version 1.0
 * @date 2020/5/25 20:14
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 请求前拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        HttpSession session = request.getSession();
        if(session == null || session.getAttribute("user") == null){
            //公共静态最终HttpStatus未授权
            //401未授权
//            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.sendRedirect("/toLogin");
            return false;
        }
        return true;
    }
}
